package com.xhis.homenurse;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import com.xhis.homenurse.models.Base_out;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 闪屏
 * by 张兵 2019/6/28
 */
public class FlashActivity extends AppCompatActivity
{
    //是否是第一次使用
    private boolean isFirstUse;
    private Intent intent;
    private static final String TAG = "FlashActivity";
    private GLobalApp mGlobalApp;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_flash);
        initView();

        handler.sendEmptyMessageDelayed(0, 2000);
    }

    private void initView()
    {
        mGlobalApp = (GLobalApp) getApplicationContext();
        mGlobalApp.addActivity(this);
        // 设置版本号；
        ((TextView) findViewById(R.id.tv_version)).setText("V" + GLobalApp.getAppVersion(this));
    }

    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            checkUpdate();
        }

    };

    /**
     * 校验更新
     */
    private void checkUpdate()
    {
        new AppUpdaterNew(this).checkVersion(0);
    }

    public void onSuccess(Base_out base_out)
    {
        if (base_out.bz.equals("0")) {
            if (base_out.getData() != null) {
                try {
                    JSONObject jsonArray = new JSONObject(base_out.getData());
                    if (jsonArray.length() > 0) {
                        int versionCode =  jsonArray.getInt("versionCode");
                        String downloadUrl = jsonArray.getString("downloadUrl");
                        if (versionCode > GLobalApp.getVersionCode(this)) {
                            if (!TextUtils.isEmpty(downloadUrl)) {
                                Intent i = new Intent(FlashActivity.this, AppUpdaterActivity.class);
                                i.putExtra("url", downloadUrl);
                                startActivity(i);
                                finish();
                            } else {
                                start();
                            }
                        } else {
                            start();
                        }
                    } else {
                        start();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                start();
            }
//            VersionCheck_out out = (VersionCheck_out) base_out;
//            if (Integer.valueOf(out.getVersionCode()) > GLobalApp.getVersionCode(this)) {
//                if (TextUtils.isEmpty(out.getDownloadUrl())) {
//                    Intent i = new Intent(FlashActivity.this, AppUpdaterActivity.class);
//                    i.putExtra("url", out.getDownloadUrl());
//                    startActivity(i);
//                    finish();
//                } else {
//                    start();
//                }
//            } else {
//                start();
//            }
        } else {
            start();
        }
    }

    public void onFailure(String errMsg)
    {
        Log.d(TAG, errMsg);
        start();
    }

    public void start()
    {
        SharedPreferences preferences = getSharedPreferences("isFirstUse", MODE_PRIVATE);
        isFirstUse = preferences.getBoolean("isFirstUse", true);

        //如果用户不是第一次使用则直接跳转到主界面,否则调转到引导界面
        if (isFirstUse) {
            intent = new Intent(FlashActivity.this, GuideActivity.class);
        } else {
            intent = new Intent(FlashActivity.this, MainActivity.class);
        }
        //实例化Editor对象
        SharedPreferences.Editor editor = preferences.edit();
        //存入数据
        editor.putBoolean("isFirstUse", false);
        //提交修改
        editor.commit();

        startActivity(intent);
        //执行一次后销毁本页面
        finish();
    }

    @Override
    protected void onDestroy()
    {
        mGlobalApp.removeActivity(this);
        super.onDestroy();
    }
}
